function [ EC ] = APEreducedata( datasetname, frames, inputtype, alongaxis)
%[ EC ] = APEreducedata( datasetname, framenumber, inputtype , alongaxis)
%
%   Extracts a single Spectrum from a Kinetic time series and saves it to a
%   new .mat file, conserving all additional information present.
%
% INPUT     datasetname     :   File name of source 
%           frames          :   [start:end]
%           inputtype       :   Start and End frames are given as data 
%                               values instead of frame index 
%                               'raman' = Ramanshift
%                               'wave'  = Wavelength
%                               'counts'= Y signal
%                               'ALS'   = ALS corrected Y signal
%           alongaxis       :   'x' = reduce Data along X (datax), 
%                                       otherwise along Y (datay)
%
% OUTPUT            EC      :   Error Code. (0=success, 1=error)

% DEPENDENCIES              - 

%  (c) F. Dworkowski, 2011
%      Version 1.0, 2011-09-07

%% ===== INITALIZE  =====
if nargin<2,
    help APEreducedata
    EC = 1;
    return
end

%% =====   CONFIG   =====
EC=0;
if ~exist('alongaxis','var')
    alongaxis = 0;
end
%% =====    CODE    =====
try load(datasetname)
catch ME
  errordlg('ERROR: file not found in "APEextractspectrum"!')
  return
end

newset = load(datasetname); % copy all information to new set before reduction
frames = round(frames);       % avoid non-integer input

%% ===== Value to Frame CONVERSION =====
if exist('inputtype','var')
    switch inputtype
        case 'raman'
            if exist('ramandatax','var')
                istart  = find(ramandatax>min(frames),1);
                iend    = find(ramandatax>max(frames),1);
                frames  = [istart:iend];
                alongaxis = 'x';
            else
                errordlg('ERROR: No "ramandatax" availabel!!')
            end
        case 'wave'
            istart  = find(datax>min(frames),1);
            iend    = find(datax>max(frames),1);
            frames  = [istart:iend];            
            alongaxis = 'x';            
        case 'counts'
            istart  = find(datay>min(frames),1);
            iend    = find(datay>max(frames),1);
            frames  = [istart:iend];
        case 'ALS'
            if exist('ALSdatay','var')
                istart  = find(ALSdatay>min(frames),1);
                iend    = find(ALSdatay>max(frames),1);
                frames  = [istart:iend];
            else
                errordlg('ERROR: No "ALSdatay" availabel!!')
            end
        otherwise
    end
end
            
%% ===== PROCESSING =====
switch alongaxis
    case 'x'
            if max(frames) > length(datay(:,1))     % check if index is available
                errordlg('ERROR: Please give correct Index for extraction!')
                EC = 1;
                return
            end

            newset.datay = datay(frames,:)
            newset.datax = datax(frames,:)

            % --- check for ALS corrected Data and process if applicable ---
            if exist('ALSdatay','var')
                disp('ALS in da house')
                newset.ALSdatay = ALSdatay(frames,:);
                newset.ALSbasey = ALSbasey(frames,:);
                newset.ALSres   = ALSres(frames,:);
            else
                disp('No ALS. Soooo sad!')
            end
            if exist('ramandatax','var')
                disp('RAMAN calibrated data lives here')
                newset.ramandatax = ramandatax(frames,:);
            else
                disp('No RAMAN shift data. Soooo sad!')
            end
            outfilename = [datasetname,'_Xreduced-',num2str(frames(1)),...
                '-',num2str(max(frames))];
            
    otherwise       % ------ DEFAULT: along Y-Axis ------
            % --- check for ALS corrected Data and process if applicable ---
            if exist('ALSdatay','var')
                disp('ALS in da house')
                if max(frames) > length(ALSdatay(1,:))     % check if index is available
                    errordlg('ERROR: Please give correct Index for extraction!')
                    EC = 1;
                    return
                end
                newset.ALSdatay = ALSdatay(:,frames);
                newset.ALSbasey = ALSbasey(:,frames);
                newset.ALSres   = ALSres(:,frames);
            else
                disp('No ALS. Soooo sad!')
            end
            if max(frames) > length(datay(1,:))     % check if index is available
                errordlg('ERROR: Please give correct Index for extraction!')
                EC = 1;
                return
            end
            newset.datay = datay(:,frames);
            outfilename = [datasetname,'_Yreduced-',num2str(frames(1)),...
                '-',num2str(max(frames))];
end
%% ===== SAVE NEW DATASET =====            
save(outfilename, '-struct', 'newset');

end % =====================================================================

